#include "CUserRole.h"
#include "../data/CPlayerOnlineData.h"
#include "../mainctrl.h"
#include "../skill/CSkillMgr.h"
#include "../friend/CFriendMgr.h"
#include "../act/CRewardBackMgr.h"

int CUserRole::InitRoleSkill(CPlayerBaseInfo& stPlayerBase,
        CPlayerDetailInfo& stPlayerDetail)
{
    _stNormalSkill.Clear();
    _stAcadSkill.Clear();
    _stMagicSkill.Clear();

    int iRet = 0;
    int iIndex = 0;

    iIndex = CMainCtrl::m_pstMain->m_stRoleConfigMgr.FindRoleInitCfg(
        stPlayerBase._ucCareer);
    CT_CHECK(iIndex, ERROR_ROLE_INIT_EMPTY_CFG);

	CRoleInitCfgData& stRoleInitCfg =
        CMainCtrl::m_pstMain->m_stRoleConfigMgr.GetRoleInitCfg(iIndex);

    unsigned int uiSkillID = stRoleInitCfg._uiNormalSkill;
    if (uiSkillID > 0)
    {
        iRet = CMainCtrl::m_pstMain->m_stSkillMgr.GetSkillByID(_stNormalSkill,
            uiSkillID, CSkill::SKILL_TYPE_NORMAL);
        CT_RETURN(iRet);
    }

    uiSkillID = stRoleInitCfg._uiMagicSkill;
    if (stPlayerDetail._stUserDBSkill._uiMagicSkillID > 0)
    {
        iRet = CMainCtrl::m_pstMain->m_stSkillMgr.GetSkillByID(_stMagicSkill,
            stPlayerDetail._stUserDBSkill._uiMagicSkillID,
            CSkill::SKILL_TYPE_MAGIC);
        CT_RETURN(iRet);
    }
    else if (uiSkillID > 0)
    {
        iRet = CMainCtrl::m_pstMain->m_stSkillMgr.GetSkillByID(_stMagicSkill,
            uiSkillID, CSkill::SKILL_TYPE_MAGIC);
        CT_RETURN(iRet);
    }

    return 0;
}

//在本接口调用地方重新计算角色二级属性加成
int CUserRole::AddExp(CPlayerBaseInfo& stPlayerBase,
        CPlayerOnlineData& stPlayerOnline,
        CPlayerDetailInfoEx1& stPlayerDetailEx1, int64 i64AddExp)
{
    if (i64AddExp <= 0)
    {
        return 0;
    }

    short& shLevel = stPlayerBase._shLevel;
    int64& i64RoleExp = stPlayerBase._i64NowExp;

    //简单校验数值
    if (i64RoleExp < 0 || shLevel < 0)
    {
        return ERROR_ROLE_LVL_EXP_INVALID;
    }

    unsigned int uiLevelExp = 0;
    int64 i64LevelExp = 0;

    int iRet = CMainCtrl::m_pstMain->m_stRoleConfigMgr.GetLevelUpExp(
            shLevel, uiLevelExp);
    CT_RETURN(iRet);

    i64LevelExp = (int64)(uiLevelExp);
    
    int64 i64Tmp = i64RoleExp + i64AddExp;

    while(i64Tmp >= i64LevelExp)
    {
        if (shLevel >= OPEN_ROLE_LEVEL)
        {
            break;//必须退出
        }
        else
        {
            i64Tmp -= i64LevelExp;
            ++shLevel;

            //QQ好友邀请
            if (shLevel - 1 < CFriendMgr::QQ_FRIEND_FRIEND_DAILY_LEVEL_UP &&
                shLevel >= CFriendMgr::QQ_FRIEND_FRIEND_DAILY_LEVEL_UP)
            {
                CMainCtrl::m_pstMain->m_stFriendMgr.DealQQFriendInnerCmd(
                    stPlayerBase._uiGID,
                    CFriendMgr::CMD_QQ_FRIEND_SUB_PREPARE_BE_INVITED,
                    CFriendMgr::CMD_QQ_FRIEND_SUB_DAILY_INVITE, 0);
            }

            if (shLevel - 1 < CFriendMgr::QQ_FRIEND_FRIEND_LEVEL_UP &&
                shLevel >= CFriendMgr::QQ_FRIEND_FRIEND_LEVEL_UP)
            {
                CMainCtrl::m_pstMain->m_stFriendMgr.DealQQFriendInnerCmd(
                    stPlayerBase._uiGID,
                    CFriendMgr::CMD_QQ_FRIEND_SUB_PREPARE_BE_INVITED,
                    CFriendMgr::CMD_QQ_FRIEND_SUB_ADD_LVL_UP_NUM, 0);
            }

            //奖励找回
            if (shLevel - 1 < CRewardBackMgr::ENM_REWARD_BACK_OPEN_LEVEL &&
                shLevel >= CRewardBackMgr::ENM_REWARD_BACK_OPEN_LEVEL)
            {
                CMainCtrl::m_pstMain->m_stRewardBackMgr.UpdateOpenZeroTime(
                    stPlayerBase, stPlayerDetailEx1,
                    CRewardBackMgr::ENM_REWARD_BACK_STAT_LEVEL);
            }
        }

        iRet = CMainCtrl::m_pstMain->m_stRoleConfigMgr.GetLevelUpExp(shLevel,
            uiLevelExp);
        CT_RETURN(iRet);

        i64LevelExp = (int64)(uiLevelExp);
    }

    i64RoleExp = i64Tmp;
    if (shLevel >= OPEN_ROLE_LEVEL)
    {
        i64RoleExp = 0;
    }

    return 0;
}

int CUserRole::ChgMagicSkill(CPlayerBaseInfo& stPlayerBase,
        CPlayerDetailInfo& stPlayerDetail, CPlayerOnlineData& stPlayerOnline,
        unsigned int uiSkillID)
{
    int iRet = 0;
    if (_stMagicSkill.GetID() == uiSkillID)
    {
        return 0;
    }

    if (!CMainCtrl::m_pstMain->m_stCommConfigLoader.IsOpen(SYS_OPEN_COND_ID_37,
            stPlayerBase, stPlayerOnline))
    {
        return ERROR_SYS_NOT_OPEN;
    }

    iRet = CMainCtrl::m_pstMain->m_stSkillMgr.GetSkillByID(_stMagicSkill,
        uiSkillID, CSkill::SKILL_TYPE_MAGIC);
    CT_RETURN(iRet);

    stPlayerDetail._stUserDBSkill._uiMagicSkillID = uiSkillID;

    return 0;
}

